﻿using System;
using fastCSharp;

namespace diantou.web.onlineJudge
{
    /// <summary>
    /// AC总分排行榜
    /// </summary>
    abstract class rankList<viewType> : web.view<viewType>
        where viewType : rankList<viewType>
    {
        /// <summary>
        /// 
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected userRank[] ranks;
        /// <summary>
        /// 当前用户排行
        /// </summary>
        protected userRank topUser;
        /// <summary>
        /// 当前用户排行
        /// </summary>
        protected userRank bottomUser;
        /// <summary>
        /// 获取排行列表
        /// </summary>
        protected abstract diantou.dataAccess.user[] users { get; }
        /// <summary>
        /// 当前用户排行
        /// </summary>
        protected abstract int currentUserRank { get; }
        /// <summary>
        /// 分页连接
        /// </summary>
        protected abstract string pagePath { get; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        protected virtual bool loadView(int page)
        {
            pageInfo.Page = page;
            diantou.dataAccess.user[] values = users;
            array.page arrayPage = new array.page(pageInfo.Count, pageInfo.Size, page);
            int rank = arrayPage.SkipCount, userId = currentUserId, index = 0, isUser = 0;
            ranks = new userRank[arrayPage.CurrentPageSize];
            foreach (diantou.dataAccess.user user in values)
            {
                if (user.Id == userId)
                {
                    ranks[index++].Set(user, ++rank, true);
                    isUser = 1;
                }
                else ranks[index++].Set(user, ++rank, false);
            }
            if (userId != 0 && isUser == 0 && (index = currentUserRank) != 0)
            {
                if (index > rank) bottomUser.Set(currentUser, index, true);
                else topUser.Set(currentUser, index, true);
            }
            if (requestHeader.IsSearchEngine) pageInfo.Path = pagePath;
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void clear()
        {
            base.clear();
            topUser.User = bottomUser.User = null;
        }
    }
    /// <summary>
    /// AC总分排行榜
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsReferer = false)]
    partial class rankList : rankList<rankList>
    {
        /// <summary>
        /// 分页连接
        /// </summary>
        private static readonly string path = diantou.dataModel.path.pub.Default.OnlineJudgeRankPath + "?";
        /// <summary>
        /// 分组菜单
        /// </summary>
        private groupMenu groupMenu
        {
            get { return new groupMenu { ViewOnly = requestHeader.IsReView }; }
        }
        /// <summary>
        /// 获取排行列表
        /// </summary>
        protected override diantou.dataAccess.user[] users
        {
            get
            {
                return diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.judge.getPointRankUserIds(pageInfo.Size = 20, pageInfo.Page, out pageInfo.Count));
            }
        }
        /// <summary>
        /// 当前用户排行
        /// </summary>
        protected override int currentUserRank { get { return currentUser.Remote.OnlineJudgeRank; } }
        /// <summary>
        /// 分页连接
        /// </summary>
        protected override string pagePath { get { return path; } }
    }
}
